package com.runvp.rest.biz;

import com.runvp.common.annotation.Log;
import com.runvp.common.annotation.ParamValid;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.page.TableDataInfo;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.utils.StringUtils;
import com.runvp.common.utils.poi.ExcelUtil;
import com.runvp.service.biz.domain.BizDeviceCargo;
import com.runvp.service.biz.service.IBizDeviceCargoService;
import com.runvp.service.biz.service.IBizGoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备端口Controller
 *
 * @author runvp
 * @date 2022-12-24
 */
@Controller
@RequestMapping("/biz/cargo")
public class BizDeviceCargoController extends BaseController {
    private String prefix = "biz/cargo";

    @Autowired
    private IBizDeviceCargoService bizDeviceCargoService;
    @Autowired
    private IBizGoodsService bizGoodsService;


    @RequiresPermissions("biz:cargo:view")
    @GetMapping()
    public String cargo(ModelMap mmap, Long deviceId) {
        mmap.put("deviceId", deviceId);
        return prefix + "/cargo";
    }

    @RequiresPermissions("biz:cargo:view")
    @GetMapping("/replenishment")
    public String replenishment(ModelMap mmap, Long deviceId) {
        mmap.put("deviceId", deviceId);
        return prefix + "/replenishment/replenishment";
    }

    /**
     * 上货
     * @param mmap
     * @param deviceId
     * @return
     */
    @RequiresPermissions("biz:cargo:view")
    @GetMapping("/puton")
    public String puton(ModelMap mmap, Long deviceId) {
        mmap.put("deviceId", deviceId);
        return prefix + "/puton/puton";
    }
    /**
     * 修改端口商品
     */
    @RequiresPermissions("biz:cargo:edit")
    @GetMapping("/puton/edit/{id}")
    public String replenishmentEdit(@PathVariable("id") Long id, ModelMap mmap) {
        BizDeviceCargo bizDeviceCargo = bizDeviceCargoService.selectBizDeviceCargoById(id);
        mmap.put("bizDeviceCargo", bizDeviceCargo);
        return prefix + "/puton/edit";
    }
    /**
     * 查询设备端口列表
     */
    @RequiresPermissions("biz:cargo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizDeviceCargo bizDeviceCargo) {
        startPage();
        List<BizDeviceCargo> list = bizDeviceCargoService.selectBizDeviceCargoList(bizDeviceCargo);
        return getDataTable(list);
    }

    /**
     * 导出设备端口列表
     */
    @RequiresPermissions("biz:cargo:export")
    @Log(title = "设备端口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizDeviceCargo bizDeviceCargo) {
        List<BizDeviceCargo> list = bizDeviceCargoService.selectBizDeviceCargoList(bizDeviceCargo);
        ExcelUtil<BizDeviceCargo> util = new ExcelUtil<BizDeviceCargo>(BizDeviceCargo.class);
        return util.exportExcel(list, "设备端口数据");
    }

    /**
     * 新增设备端口
     */
    @GetMapping("/add")
    public String add(ModelMap mmap,Long deviceId) {
        mmap.put("deviceId",deviceId);
        return prefix + "/add";
    }

    /**
     * 新增保存设备端口
     */
    @ParamValid
    @RequiresPermissions("biz:cargo:add")
    @Log(title = "设备端口", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizDeviceCargo bizDeviceCargo) {
        return toAjax(bizDeviceCargoService.createBizDeviceCargo(bizDeviceCargo));
    }

    /**
     * 新增保存设备端口
     */
    @RequiresPermissions("biz:cargo:edit")
    @Log(title = "设备端口初始化", businessType = BusinessType.INSERT)
    @GetMapping("/initCargo")
    @ResponseBody
    public AjaxResult initCargo(Long deviceId) {
        String deviceSn = bizDeviceCargoService.initCargo(deviceId);
        if(StringUtils.isNotBlank(deviceSn)){
            //初始化收费方案
            bizGoodsService.initFee(deviceSn);
        }
        return toAjax(1);
    }
    /**
     * 下发收费方案
     */
    @RequiresPermissions("biz:cargo:sendFee")
    @Log(title = "下发收费方案", businessType = BusinessType.OTHER)
    @GetMapping("/sendFee")
    @ResponseBody
    public AjaxResult sendFee(Long deviceId) {
        bizDeviceCargoService.sendFee(deviceId);
        return toAjax(1);
    }
    /**
     * 下发收费方案
     */
    @RequiresPermissions("biz:cargo:sendFee")
    @Log(title = "下发收费方案", businessType = BusinessType.OTHER)
    @GetMapping("/sendFeeBySn")
    @ResponseBody
    public AjaxResult sendFeeBySn(String deviceSn) {
        bizDeviceCargoService.sendFee(deviceSn);
        return toAjax(1);
    }
    /**
     * 新增保存设备端口
     */
    @RequiresPermissions("biz:cargo:edit")
    @Log(title = "一键补满", businessType = BusinessType.INSERT)
    @GetMapping("/replenish")
    @ResponseBody
    public AjaxResult replenish(Long deviceId) {
        bizDeviceCargoService.replenish(deviceId);
        return toAjax(1);
    }
    /**
     * 刷新大屏商品列表
     */
    @RequiresPermissions("biz:cargo:edit")
    @Log(title = "刷新大屏商品列表", businessType = BusinessType.INSERT)
    @GetMapping("/refresh/list")
    @ResponseBody
    public AjaxResult refreshList(Long deviceId) {
        bizDeviceCargoService.refreshList(deviceId);
        return toAjax(1);
    }
    /**
     * 充电开启
     */
    @RequiresPermissions("biz:cargo:edit")
    @Log(title = "充电开启测试", businessType = BusinessType.OTHER)
    @GetMapping("/charge/start")
    @ResponseBody
    public AjaxResult chargeStart(Long id) {
        return toAjax(bizDeviceCargoService.chargeStart(id));
    }
    /**
     * 充电停止
     */
    @RequiresPermissions("biz:cargo:edit")
    @Log(title = "充电停止测试", businessType = BusinessType.OTHER)
    @GetMapping("/charge/end")
    @ResponseBody
    public AjaxResult chargeEnd(Long id) {
        return toAjax(bizDeviceCargoService.chargeEnd(id));
    }
    /**
     * 修改设备端口
     */
    @RequiresPermissions("biz:cargo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizDeviceCargo bizDeviceCargo = bizDeviceCargoService.selectBizDeviceCargoById(id);
        mmap.put("bizDeviceCargo", bizDeviceCargo);
        return prefix + "/edit";
    }

    /**
     * 设备端口详情
     */
    @RequiresPermissions("biz:cargo:edit")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        BizDeviceCargo bizDeviceCargo = bizDeviceCargoService.selectBizDeviceCargoById(id);
        mmap.put("bizDeviceCargo", bizDeviceCargo);
        return prefix + "/detail";
    }

    /**
     * 修改保存设备端口
     */
    @ParamValid
    @RequiresPermissions("biz:cargo:edit")
    @Log(title = "设备端口", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizDeviceCargo bizDeviceCargo) {
        return toAjax(bizDeviceCargoService.updateBizDeviceCargo(bizDeviceCargo));
    }

    /**
     * 删除设备端口
     */
    @RequiresPermissions("biz:cargo:remove")
    @Log(title = "设备端口", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizDeviceCargoService.deleteBizDeviceCargoByIds(ids));
    }
}
